// import { getSignatureParam } from './auth'
import { useAuth } from '~/stores/auth/index'

export default async function <Type>(url: string, options: any) {
    const { public: { NUXT_APP_API_URL } } = useRuntimeConfig()
    const { clearInfo } = useAuth()
    const { token } = storeToRefs(useAuth())

    return $fetch<ResponseData<Type>>(url, {
        ...options,
        baseURL: NUXT_APP_API_URL,
        onRequest({ request, options }) {
            if (token.value) {
                options.headers.set('Authorization', token.value)
                // options.headers.set('x-Ins-Token', token.value)
            }
            // const { timestamp, signature, nonceStr } = getSignatureParam()
            // options.headers.set('X-Ins-Timestamp', (timestamp as any))
            // options.headers.set('X-Ins-Signature', (signature as any))
            // options.headers.set('X-Ins-Nonce', (nonceStr as any))
        },
        onResponse({ response }) {
            const data = response._data
            if (data.code === 401) {
                clearInfo()
            }
        }
    })
} 